// pages/contact/joinus3.js
const app = getApp()
const nt_util = require('../../utils/nt_util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    queryBean: {},
    dptp: '',
    yyzz: '',
    dptp_url: '',
    yyzz_url: '',
    phoneBak: '',
    remark:'',
    invitationCode: '',
    btn_saveStoreInfo:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    nt_util.that = this;
    var queryBean = JSON.parse(options.queryBean);
    nt_util.that.setData({
      queryBean: queryBean
    })
    
  },
  chooseImage: function (e) {
    var that = this;
    nt_util.chooseImage(function (res) {
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
      var dataset = e.currentTarget.dataset;
      if (dataset.type == "1") {
        that.setData({
          yyzz: res[0]
        });
      } else {
        that.setData({
          dptp: res[0]
        });
      }
    }, function (res) {

    });
  },
  previewImage: function (e) {
    nt_util.previewImage(e.currentTarget.id, [e.currentTarget.id]);
  },
  // 保存门店信息
  saveStoreInfo: function (e) {
    var that = this;


    if (!that.checkParams()) {
      return false;
    }
    if (that.data.btn_saveStoreInfo) {
      wx.showModal({
        title: '提示',
        content: '您已提交，等待后台审核！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    that.setData({
      btn_saveStoreInfo: true,
    });


    nt_util.uploadFile('api/life/v1/uploadImage', that.data.dptp, '', { type: 'store' }, function (res) {
      that.setData({
        dptp_url: res.data.imageUrl
      })
      nt_util.uploadFile('api/life/v1/uploadImage', that.data.yyzz, '', { type: 'scopePic' }, function (res2) {
        that.setData({
          yyzz_url: res2.data.imageUrl
        })

        var params = {
          address: that.data.queryBean.address + that.data.queryBean.addressInfo,
          name: that.data.queryBean.queryBean2.name,
          lat: that.data.queryBean.location.lat,
          lng: that.data.queryBean.location.lng,
          phone: that.data.queryBean.queryBean2.phone,
          storeName: that.data.queryBean.queryBean2.storeName,
          phoneBak: that.data.phoneBak,
          scopePic: that.data.yyzz_url,
          storePic: that.data.dptp_url,
          typeId: that.data.queryBean.queryBean2.typeId,
          remark: that.data.remark,
          invitationCode: that.data.invitationCode,
        };
        nt_util.httpRequest('api/life/v1/saveStoreInfo', 'post', params, function (res) {
          that.setData({
            dptp: '',
            yyzz: '',
          })

          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1500,
            complete: function (res) {
              setTimeout(function () {
                wx.setStorage({
                  key: "home_reload",
                  data: "true"
                })
                wx.switchTab({
                  url: '/pages/contact/person'
                })
              }, 1500);
            }
          })

        });
      }, function (fail) {

      });
    }, function (fail) {

    });

  },

  checkParams: function () {
    var that = this;

    if (!that.data.dptp) {
      wx.showToast({
        title: '店铺图片不能空',
      })
      return false;
    }
    if (!that.data.yyzz) {
      wx.showToast({
        title: '营业执照不能空',
      })
      return false;
    }
    if (!that.data.remark) {
      wx.showToast({
        title: '自我介绍不能空',
      })
      return false;
    }
    if (!that.data.phoneBak) {
      wx.showToast({
        title: '备用号码不能空',
      })
      return false;
    }
    
    return true;
  },
  bindKeyInput: function (e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    var data_type = e.detail.value;
    switch (dataset.type) {
      case 'remark':
        that.setData({
          remark: data_type
        });
        break;
      case 'phoneBak':
        that.setData({
          phoneBak: data_type
        });
        break;
      case 'invitationCode':
        that.setData({
          invitationCode: data_type
        });
        break;
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  }
})